package com.ruoyi.material.controller.file;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.material.service.IBaseUpAndDownLoadService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

/**
 * @program: MMS
 * @description: 文件上传下载的接口
 * @author: cyw
 * @create: 2021-07-24 14:02
 **/
@RestController
@RequestMapping("/file/file")
public class FileUpAndDownLoadController {
    private Logger log = LoggerFactory.getLogger(FileUpAndDownLoadController.class);
    @Autowired
    private IBaseUpAndDownLoadService baseLabelService;

    @Autowired
    private TokenService tokenService;
    /**
     * 压缩包的上传
     * @param zipFile
     * @param codeMap
     * @return
     * @throws Exception
     */
    @PostMapping("/uploadZipFile")
    public AjaxResult uploadFile(@RequestParam("file") MultipartFile zipFile , @RequestParam Map<String,String> codeMap) throws Exception {
        // todo 一些数据的校验和报错异常处理返回给前端
        String code = codeMap.get("code") == null ? "up" : codeMap.get("code");
        String[] labels = new String[]{"晴天","阴天","雾天"};
        log.info("zip File [{}]",zipFile);
        log.info("code",code);

        baseLabelService.uploadZipFile(zipFile,code,getCurrentUserName(),labels);
        return AjaxResult.success();
    }
    /**
     * 获取当前用户名
     */
    public String getCurrentUserName() {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        return  loginUser == null ? "tourist" : loginUser.getUsername();
    }
}
